package com.jing.product.controller;

import com.jing.common.core.domain.R;
import com.jing.common.core.web.controller.BaseController;
import com.jing.common.core.web.page.TableDataInfo;
import com.jing.product.domain.dto.ProductCategoryRecursionDTO;
import com.jing.product.domain.dto.ProductDTO;
import com.jing.product.domain.dto.ProductResultDTO;
import com.jing.product.domain.po.Product;
import com.jing.product.domain.vo.ProductVO;
import com.jing.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.validation.Valid;
import java.util.*;

/**
 * @Author：HaoYunde
 * @Package：com.jing.product.controller
 * @Project：Jing
 * @name：ProductController
 * @Date：2024/5/23 20:25
 */
@RestController
@RequestMapping("/product")
@Validated
@Api(tags = "商品表")
public class ProductController extends BaseController {

    @Autowired
    ProductService productService;
    @Autowired
    private RestTemplate restTemplate;


    /**
     * 查询商品列表
     * @return
     */
    @PostMapping("/findProductList")
    @ApiOperation(value = "查询商品列表")
    public R findProductList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                             @RequestBody ProductVO productVO){
        startPage();
        List<ProductDTO> list=productService.findProductList(productVO);
        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }



    /**
     * 删除商品表
     * @param id
     * @return
     */
    @PostMapping("/delect")
    @ApiOperation(value = "删除商品表")
    public R delect(@Valid @RequestParam Integer id) {
        productService.delect(id);
        return R.ok();
    }


    /**
     * 修改品牌表
     * @param product
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改品牌表")
    public R update(@Valid @RequestBody ProductDTO product){
        productService.update(product);
        return R.ok();
    }


//    /**
//     * 天气预报接口
//     * @return
//     */
//    @GetMapping("weather")
//    @ApiOperation(value = "天气预报接口")
//    public R weather(){
//        //创建请求头
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.add("13818798","JM0wI3mt");
//        // 构建参数
//        Map<String, Object> queryParams = new HashMap<>();
//        // 可以根据API要求添加其他参数，比如日期等
//        queryParams.put("appid","哈哈哈哈");
//        queryParams.put("appsecret","1");
//        queryParams.put("adcode","1");
//        queryParams.put("cityid","1");
//        queryParams.put("city","1");
//        queryParams.put("ip","1");
//        queryParams.put("callback","1");
//        queryParams.put("vue","1");
//        queryParams.put("unescape","1");
//        // 发送GET请求
//        String apiUrl = "http://v1.yiketianqi.com/free/month";
//        HttpEntity httpEntity = new HttpEntity<>(queryParams, httpHeaders);
//        R forObject = restTemplate.getForObject(apiUrl, R.class, httpEntity);
//        return  R.ok(forObject);
//    }


    /**
     * 添加商品
     * @param productDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加商品")
    public R add(@RequestBody @Valid ProductDTO productDTO) {
         productService.addProduct(productDTO);
         return R.ok("添加商品");
    }
    /**
     * 修改商品是否上架
     * @param id
     * @param publishStatus
     * @return
     */
    @GetMapping("/updatepublishStatus")
    @ApiOperation(value = "修改商品是否上架")
    public R updatepublishStatus(@Valid @RequestParam Integer id,@RequestParam Integer publishStatus){
        productService.updatepublishStatus(id,publishStatus);
        return R.ok("修改商品是否上架成功");
    }
    /**
     * 修改商品是否新品
     * @param id
     * @param newStatus
     * @return
     */
    @GetMapping("/updatenewStatus")
    @ApiOperation(value = "修改商品是否新品")
    public R updatenewStatus(@Valid @RequestParam Integer id,@RequestParam Integer newStatus){
        productService.updatenewStatus(id,newStatus);
        return R.ok("修改商品是否新品");
    }


    /**
     * 修改商品是否推荐
     * @param id
     * @param recommandStatus
     * @return
     */
    @GetMapping("/updaterecommandStatus")
    @ApiOperation(value = "修改商品是否推荐")
    public R updaterecommandStatus(@Valid @RequestParam Integer id,@RequestParam Integer recommandStatus){
        productService.updaterecommandStatus(id,recommandStatus);
        return R.ok("修改商品是否推荐");
    }




    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @ApiOperation("根据商品id获取商品编辑信息")
    @GetMapping(value = "/updateInfo")
    public R<ProductResultDTO> getUpdateInfo(@RequestParam Integer id) {
        ProductResultDTO productResult = productService.getUpdateInfo(id);
        return R.ok(productResult);
    }



    @ApiOperation(value = "递归")
    @GetMapping("recursion/{id}")
    public R<ArrayList<ProductCategoryRecursionDTO>> recursion(@PathVariable Integer id){
        ArrayList<ProductCategoryRecursionDTO> list=productService.recursion(id);
        return R.ok(list);
    }




}
